package com.ruihang.generalibrary.ui.adapter;

import android.content.Context;
import android.util.SparseArray;

import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.IHolderClick;

import java.util.List;

/**
 * Created by Ly on 2016/12/9.
 */
public abstract class BaseHolderAbsAdapter<T, VH extends BaseRecylerHolder<T>> extends BaseRecylerAdapter<T, VH> {
    /**
     * 是否是选择模式
     */
    protected boolean canSelector = false;

    protected IHolderClick<VH> holderClick;

    protected VH selectedHolder;

    /**
     * 是否是编辑状态
     */
    protected boolean isEdting;
    /**
     * 是否倒序
     */
    protected boolean isReverse;

    public BaseHolderAbsAdapter(Context context, List<T> list) {
        super(context, list);
    }

    protected IHolderClick<VH> defHolderClick = new IHolderClick<VH>() {
        @Override
        public void onHolderClick(VH vh, int position) {
            onVHolderClick(vh, position);
        }
    };

    @Override
    public void onBindViewHolder(VH holder, int position) {
        T t = getItem(position);
        if (canSelector) {
            holder.setData(t, position, isSelected(t, position), isEdting);
        } else {
            holder.setData(t, isEdting);
        }

    }

    protected int getOldSelectPos() {
        return 0;
    }

    protected void onVHolderClick(VH vh, int position) {
        if (holderClick != null) {
            if (selectedHolder != null && vh != selectedHolder) {
                selectedHolder.onHolderUnSelect(position, getOldSelectPos());
            }
            holderClick.onHolderClick(vh, position);
            selectedHolder = vh;
        }
    }

    protected boolean isSelected(T t, int position) {
        return false;
    }

    public BaseHolderAbsAdapter setCanSelector(boolean canSelector) {
        this.canSelector = canSelector;
        return this;
    }

    public BaseHolderAbsAdapter setHolderClick(IHolderClick<VH> holderClick) {
        this.holderClick = holderClick;
        return this;
    }

    public synchronized void setEdting(boolean edting) {
        boolean b = this.isEdting != edting;
        isEdting = edting;
        if (b) {
            if (!edting) {
                resetSelectors();
            }
            notifyDataSetChanged();
        }
    }

    protected void resetSelectors() {
    }

    public int getSelectorCount() {
        return 0;
    }

    public SparseArray<?> getSelectors() {
        return null;
    }

    public List<T> getSelectList() {
        return null;
    }

    @Override
    public BaseHolderAbsAdapter setData(List<T> data) {
        if (isEdting) {
            setEdting(false);
        }
        super.setData(data);
        return this;
    }

    @Override
    public void onViewAttachedToWindow(VH holder) {
        super.onViewAttachedToWindow(holder);
        lifeAttach(holder);
    }

    @Override
    public void onViewDetachedFromWindow(VH holder) {
        lifeDettach(holder);
        super.onViewDetachedFromWindow(holder);
    }

    protected void lifeAttach(VH holder) {
        if (holder instanceof IAndroidLittleLife) {
            IAndroidLittleLife ia = (IAndroidLittleLife) holder;
            ia.setLifeMode(1);
            if (ia.hasInManager()) {
                ia.onIResume();
            } else {
                addLife(ia);
            }
        }
    }

    protected void lifeDettach(VH holder) {
        if (holder instanceof IAndroidLittleLife) {
            IAndroidLittleLife ia = (IAndroidLittleLife) holder;
            if (ia.hasInManager()) {
                ia.onIPause();
            }
            ia.setLifeMode(0);
        }
    }

    protected void addLife(IAndroidLittleLife ia) {
    }

    public void setReverse(boolean reverse) {
        isReverse = reverse;
    }
}
